package com.jsgt.greekfire.ui.mine.user.changesecuritypwd

import android.text.Selection
import android.text.Spannable
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.widget.CompoundButton
import android.widget.EditText
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.event.TransferEvent
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_change_security_pwd.*
import kotlinx.android.synthetic.main.activity_change_security_pwd.complete_tv
import kotlinx.android.synthetic.main.activity_change_trading_pwd.*
import org.greenrobot.eventbus.EventBus

/**
 * 修改安全密码
 * author fhx
 * create time 2020/4/23 15:23
 **/
class ChangeSecurityPwdActivity : BaseActivity() {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }

    override fun getLayoutId(): Int = R.layout.activity_change_security_pwd

    override fun main() {
        title = getString(R.string.change_security_pwd)

        current_pwd_cb.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                if (isChecked) {
                    current_pwd_et.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    current_pwd_et.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                endSelection(current_pwd_et)
            }
        })
        new_pwd_cb.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                if (isChecked) {
                    new_pwd_et.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    new_pwd_et.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                endSelection(new_pwd_et)
            }
        })
        re_new_pwd_cb.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                if (isChecked) {
                    re_new_pwd_et.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    re_new_pwd_et.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                endSelection(re_new_pwd_et)
            }
        })

        complete_tv.setOnClickListener {
            submit()
        }
        userViewModel.updateSecurityPwdLiveData.observe(this, Observer {
            showProgress(false)
            ToastUtils.show(this, getString(R.string.complete))
            EventBus.getDefault().post(TransferEvent(""))
            finish()
        })

        userViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })
    }

    private fun submit() {
        val currentPwd = current_pwd_et.text.toString().trim()
        val newPwd = new_pwd_et.text.toString().trim()
        val reNewPwd = re_new_pwd_et.text.toString().trim()
        if (TextUtils.equals(currentPwd, newPwd)) {
            ToastUtils.show(this, getString(R.string.error_password_same))
            return
        }
        if (TextUtils.isEmpty(currentPwd)) {
            ToastUtils.show(this, getString(R.string.input_security_password))
            return
        }
        if (TextUtils.isEmpty(newPwd)) {
            ToastUtils.show(this, getString(R.string.input_security_password_new))
            return
        }
        if (newPwd.length < 6) {
            ToastUtils.show(this, getString(R.string.wallet_safe_pwd_warning))
            return
        }
        if (!TextUtils.equals(newPwd, reNewPwd)) {
            ToastUtils.show(this, getString(R.string.wallet_pwd_warning))
            return
        }
        showProgress(true)
        userViewModel.updateSecurityPwd(currentPwd, newPwd)
    }

    /**
     * 设置EditText游标至末尾
     */
    fun endSelection(editText: EditText) {
        val text: Spannable? = editText.text
        if (text != null) {
            Selection.setSelection(text, text.length)
        }
    }
}